package cn.com.open.itembank.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


/**
 * Created by lucky on 2017/8/3.
 */
public class JsonUtil {
    /**
     * Gets json.
     *
     * @param obj the object
     * @return the json
     */
    public static String getJson(Object obj) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().create();
        return gson.toJson(obj);
    }

    /**
     * Gets json without disable html escaping.
     *
     * @param obj the obj
     * @return the json without disable html escaping
     */
    public static String getJsonWithoutDisableHtmlEscaping(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    /**
     * From json t.
     *
     * @param <T>        the type parameter
     * @param jsonString the json string
     * @param type       the type
     * @return the t
     */
    public static <T> T fromJson(String jsonString, Class<T> type) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().create();
        return gson.fromJson(jsonString, type);
    }
}
